package com.fyy.baiduimg.baiduapi;

import com.alibaba.fastjson.JSONObject;
import com.fyy.baiduimg.utils.Base64Util;
import com.fyy.baiduimg.utils.FileUtil;
import com.fyy.baiduimg.utils.HttpUtil;
import java.net.URLEncoder;

/**
 * @Author fyy
 * @Description
 * @Date 2021/4/14 17:07
 */
public class SampleTest {

    public static final String APP_ID = "23990329";
    public static final String API_KEY = "mpzih3I4bKAlUXgEdG6sMism";
    public static final String SECRET_KEY = "xdQkBW7f2U41tKCY2FtVoNSxV0xEgDD5";

    public static void main(String[] args) {

        String url = "https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans";

        try {
            String filePath = "F:\\2.jpg";
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "option=" + "cartoon" + "&image=" + imgParam;
            String accessToken = AuthService.getAuth(API_KEY,SECRET_KEY);

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(JSONObject.parseObject(result).get("image"));

            Base64Util.base64ToFile("F:\\",JSONObject.parseObject(result).get("image").toString(),"3.jpg");

        }catch (Exception e){
            e.printStackTrace();
        }


    }

}
